<template>
  <el-card class="box-card">
    <el-form ref="queryForm" :model="query" :inline="true">
      <el-form-item label="标签名称" prop="name">
        <el-input v-model.trim="query.conditions.tagName" placeholder="请输入标签名称" clearable @change="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="name">
        <el-select style="width: 100px" v-model="query.conditions.enabledState" placeholder="全部">
          <el-option label="禁用" :value="0"></el-option>
          <el-option label="启用" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb20">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" v-has-permi="['/wx/menu/postAddWxTag']" @click="handleAdd()">新增</el-button>
      </el-col>
    </el-row>

    <el-card class="box-card">
      <el-table v-loading="listLoading" :data="dataList" stripe border row-key="id">
        <el-table-column label="标签名称" align="center" prop="tagName">
          <template slot-scope="{row}">
            <span>{{ row.tagName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联菜单" align="center" prop="tagName">
          <template slot-scope="{row}">
            <span>{{ row.menuName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="{row}">
            <el-switch
              v-has-permi="['/wx/menu/postUpdateWxTag']"
              v-model="row.enabledState"
              :active-value="1"
              :inactive-value="0"
              @change="uploadEnabledState(row, $event)"
              >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="{row}">
            <span>{{ row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-has-permi="['/wx/menu/postUpdateWxTag']" type="text" @click="handleUpdate(row)">编辑</el-button>
            <el-button v-has-permi="['/wx/menu/postDelWxTag']" type="text" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="query.pageNum" :limit.sync="query.pageSize" @pagination="handleQuery" />
    </el-card>

    <!-- 添加或修改菜单 -->
    <el-dialog :title="(formModel.id?'编辑':'新增')+'标签'" :visible.sync="formVisible" width="390px" append-to-body>
      <el-form ref="formModel" :model="formModel" :rules="formModelRules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标签名称" prop="name">
              <el-input v-model.trim="formModel.name" style="width: 200px" clearable placeholder="请输入标签名称" maxlength="12" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible=!formVisible;reset;">取 消</el-button>
        <el-button :loading="submitLoading" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { signListPage, addSign, delSign, editSign } from '@/api/wechatService/sign'

export default {
  name: "index",
  data () {
    return {
      total: 0,
      // 列表数据
      query: {
        pageNum: 1,
        pageSize: 10,
        conditions: {
          tagName: '',
          enabledState: ''
        }
      },
      cs: 3,
      dataList: [],
      menuList: [
        {
          key: 1,
          value: '选项1'
        },
        {
          key: 2,
          value: '选项2'
        },
        {
          key: 3,
          value: '选项3'
        }
      ],
      listLoading: false,
      // 新增/编辑用户
      formVisible: false,
      submitLoading: false,
      formModel: {},
      formModelRules: {
        name: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    handleQuery () {
      this.listLoading = true
      let params = {
        ...this.query
      }
      signListPage(params).then(res => {
        this.dataList = res.data.records
        this.total = res.data.total
        this.listLoading = false
      }).catch(err => {
        if (err.code === -103) {
          this.$router.push('/403')
        }
        this.listLoading = false
      })
    },
    resetQuery () {
      this.resetForm('queryForm')
      this.query = {
        pageNum: 1,
        pageSize: 10,
        conditions: {
          tagName: '',
          enabledState: ''
        }
      }
      this.handleQuery()
    },
    handleAdd(row) {
      this.reset()
      if (row) {
        this.formModel.id = row.id
        this.formModel.name = row.tagName
      }
      this.formVisible = !this.formVisible
    },
    handleUpdate (row) {
      this.handleAdd(row)
    },
    handleDelete (id) {
      this.$confirm(`请确认是否要删除该标签?`, { type: 'warning' }).then(() => {
        let params = {
          id: id
        }
        delSign(params).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      })
    },
    uploadEnabledState(row, enabledState) {
      this.$confirm(`请确认是否要${enabledState===1?'启用':'禁用'}该标签?`, { type: 'warning' }).then(() => {
        editSign({
          id: row.id,
          enabledState
        }).then(() => {
          this.$message.success(`${enabledState===1?'已启用':'已禁用'}`)
        }).catch(() => {
          this.$set(row, 'enabledState', enabledState===1?0:1)
        })
      }).catch(() => {
        this.$set(row, 'enabledState', enabledState===1?0:1)
      })
    },
    // 新增/编辑
    submitForm () {
      this.submitLoading = true
      this.$refs["formModel"].validate(async valid => {
        if (valid) {
          let params = {}
          if(this.formModel.id){
            params.id = this.formModel.id
          }
          params.tagName = this.formModel.name
          try {
            if (this.formModel.id) {
              await editSign(params)
              this.$message.success('修改成功')
            } else {
              await addSign(params)
              this.$message.success('新增成功')
            }
            this.formVisible = !this.formVisible
            this.handleQuery()
            this.submitLoading = false
          } catch (error) {
            this.submitLoading = false
          }
        } else {
          this.submitLoading = false
        }
      })
    },
    // 表单重置
    reset () {
      this.formModel = {
        id: '',
        name: ''
      }
      this.resetForm("formModel")
    },
  },
  mounted () {
    this.handleQuery()
  },
}
</script>

<style scoped>
.dh {
  width: 0;
  height: 100px;
  background-color: #1c84c6;
  transition: all 1s;
}
.dh.show {
  width: 200px;
}
.dh.hide {
  width: 0;
}
</style>
